/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cda.TestMenu;

import cda.TestMenu.Map;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.math.Vector2;

/**
 *
 * @author Shyzkanza
 */
public class Game implements ApplicationListener {

    private static final int        FRAME_COLS = 32;        // #1
    private static final int        FRAME_ROWS = 32;        // #2

    Animation                       walkAnimationDroite;    // #3
    Animation                       walkAnimationGauche;    // #4
    
    Animation                       walkAnimationStopDroite;    // #3
    Animation                       walkAnimationStopGauche;    // #4
    
    TextureRegion[]                 walkFramesStopDroite;       // #5
    TextureRegion[]                 walkFramesStopGauche;       // #6
    
    TextureRegion[]                 walkFramesDroite;       // #5
    TextureRegion[]                 walkFramesGauche;       // #6
    
    Texture                         walkSheet;              // #7
    SpriteBatch                     spriteBatch;            // #8
    TextureRegion                   currentFrame;           // #9
    
    CharSequence                    str1;                   // #10
    private BitmapFont              font;                   // #11

    float                           stateTime;              // #12
    
    cda.TestMenu.Map                map;                    // #13
    
    int                             posX;
    int                             posY;
    
    private Sprite                  sprite;
    private Texture                 fixeDroit;
    private Texture                 fixeGauche;
    
    boolean                         touche = false;
    boolean                         saut  = false;
    boolean                         droite = true;
    boolean                         gauche = false;
    
    public void create() {
        
        font = new BitmapFont(Gdx.files.internal("data/font16.fnt"),Gdx.files.internal("data/font16.png"), false);
        
        // On charge l'image en mémoire
        walkSheet = new Texture(Gdx.files.internal("personnages.png"));
        
        TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth() / FRAME_COLS, walkSheet.getHeight() / FRAME_ROWS);
        
        walkFramesDroite = new TextureRegion[2];
        walkFramesGauche = new TextureRegion[2];
        walkFramesStopDroite = new TextureRegion[1];
        walkFramesStopGauche = new TextureRegion[1];
        
        walkFramesDroite[0] = tmp[0][2];
        walkFramesDroite[1] = tmp[0][3];
        walkFramesGauche[0] = tmp[0][4];
        walkFramesGauche[1] = tmp[0][5];
        walkFramesStopDroite[0] = tmp[0][0];
        walkFramesStopGauche[0] = tmp[0][1];
        
        walkAnimationDroite = new Animation(0.200f, walkFramesDroite);
        walkAnimationGauche = new Animation(0.200f, walkFramesGauche);
        walkAnimationStopDroite = new Animation(1.0f, walkFramesStopDroite);
        walkAnimationStopGauche = new Animation(1.0f, walkFramesStopGauche);
        
        spriteBatch = new SpriteBatch();
        
        stateTime = 0f;
        
        posX = 50;
        posY = 280;
        
        map = new Map();
    }

    public void resize(int width, int height) {
        
    }

    public void render() {
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        
        stateTime += Gdx.graphics.getDeltaTime();
        
        
        if(map.Collide(new Vector2(posX,Gdx.graphics.getHeight()+5 - posY)) && !saut){
            posY --;
            if(map.Collide(new Vector2(posX,Gdx.graphics.getHeight()+5 - posY))){
                posY --;
            }
        }
        
        if(Gdx.input.isTouched()){
            if(( Gdx.input.getX() > 432) && ((Gdx.graphics.getHeight() - Gdx.input.getY()) < 112 )){
                droite = true;
                gauche = false;
                touche = true;
                posX += 2;
                
            }else if(( Gdx.input.getX() < 100) && ((Gdx.graphics.getHeight() - Gdx.input.getY()) < 112 )){
                droite = false;
                gauche = true;
                touche = true;
                posX -= 2;
            }else if(( Gdx.input.getX() > 100) && ( Gdx.input.getX() < 432)&& ((Gdx.graphics.getHeight() - Gdx.input.getY()) < 112 )){
                posY += 2;
                saut = true;
                touche = true;
            }else{
                touche = false;
                saut = false;
            }
        }else{
            touche = false;
            saut = false;
        }
        
        if(droite && touche){
            currentFrame = walkAnimationDroite.getKeyFrame(stateTime, true);
        }else if(droite && !touche){
            currentFrame = walkAnimationStopDroite.getKeyFrame(stateTime, true);
        }else if(gauche && touche){
            currentFrame = walkAnimationGauche.getKeyFrame(stateTime, true);
        }else if(gauche && !touche){
            currentFrame = walkAnimationStopGauche.getKeyFrame(stateTime, true);
        }
        
        str1 = "x : "+posX+" - y :"+posY;
        
        spriteBatch.begin();
        
        font.draw(spriteBatch, str1, 1, 132);
        map.Draw(spriteBatch);
        
        spriteBatch.draw(currentFrame, posX, posY);
        
        spriteBatch.end();
    }

    public void pause() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void resume() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void dispose() {

    }
}
